package com.hzhr.iot.web.aop;

import com.hzhr.iot.core.annotation.Author;
import com.hzhr.iot.core.constant.HzhrHeaderNames;
import com.hzhr.iot.core.exception.ServiceException;
import com.hzhr.iot.login.sdk.model.userCard.UserCardModel;
import com.hzhr.iot.login.sdk.service.Login_UserCardService;
import com.hzhr.iot.web.constant.ReturnCodeConst;
import com.hzhr.iot.web.model.response.BaseResponse;
import com.hzhr.iot.web.util.StringHelper;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;

@Aspect
@Component
public class AuthorAspect {
    @Autowired
    HttpServletRequest request;

    @Autowired
    Login_UserCardService login_userCardService;

    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Pointcut("execution(* com.hzhr.iot.web.controller..*(..))")
    public void permissionPointCut() {
    }

    @Before("permissionPointCut()")
    public void doBefore(JoinPoint joinPoint) throws Throwable {
    }

    @AfterReturning(returning = "ret", pointcut = "permissionPointCut()")
    public void doAfterReturning(Object ret) throws Throwable {
    }

    @Around("permissionPointCut()&&@annotation(author)")
    public Object doAround(ProceedingJoinPoint joinPoint, Author author) throws Throwable {
        try {
            String uat = request.getParameter("uat");
            String appcode = request.getParameter("appcode");
            if (StringHelper.isTrimEmpty(uat)) {
                return new BaseResponse(ReturnCodeConst.NO_LOGIN, "请登录后重试");
            }

            UserCardModel userCard = login_userCardService.getUserCard(null, uat, null).getModel();
            if (userCard == null)
            {
                return new BaseResponse(ReturnCodeConst.NO_LOGIN, "请登录后重试");
            }

            request.setAttribute(HzhrHeaderNames.USER_ID, userCard.getUserId());
            request.setAttribute(HzhrHeaderNames.USER_TYPE, userCard.getType());
            request.setAttribute(HzhrHeaderNames.USER_CODE, userCard.getUserCode());
            request.setAttribute(HzhrHeaderNames.USER_NAME, userCard.getUserName());
            request.setAttribute(HzhrHeaderNames.DEPT_ID, userCard.getDeptId());
            request.setAttribute(HzhrHeaderNames.COMPANY_ID, userCard.getCompanyId());

            //endregion
            //执行方法
            return joinPoint.proceed();
        } catch (ServiceException e) {
            String code = e.getCode();
            if ("2".equals(code)) {
                return new BaseResponse(ReturnCodeConst.NO_LOGIN, "请登录后重试");
            }

            String errorMessage = e.getServiceErrors().getMessage();
            return new BaseResponse(ReturnCodeConst.ERROR, errorMessage, errorMessage);
        } catch (Exception e) {
            logger.error("接口调用时，出现异常", e);
            return new BaseResponse(ReturnCodeConst.ERROR,e.getMessage(), "服务器繁忙，请稍后再试");
        }
    }
}
